import 'package:cached_network_image/cached_network_image.dart';
import 'package:changxindianzishangwu/dao/save_some_message_dao.dart';
import 'package:changxindianzishangwu/model/address_book_model.dart';
import 'package:changxindianzishangwu/model/group_message_list_model.dart';
import 'package:changxindianzishangwu/pages/message/chat/video_message_page.dart';
import 'package:changxindianzishangwu/pages/my/settings/send_message_all_page.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/widget/PhotoViewGalleryScreen.dart';
import 'package:changxindianzishangwu/widget/get_message_widget.dart';
import 'package:changxindianzishangwu/widget/loding.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/utils.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_image.dart';
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';

class SendSomeMessagePage extends StatefulWidget {
  GroupMessageListModel someMessageModel;
  SendSomeMessagePage({Key key, this.someMessageModel}) : super(key: key);

  @override
  _SendSomeMessagePageState createState() => _SendSomeMessagePageState();
}

class _SendSomeMessagePageState extends State<SendSomeMessagePage> {
  @override
  Widget build(BuildContext context) {
    if (topWidget == []) {
      return Loading();
    } else {
      return Scaffold(
        appBar: AppBar(
          elevation: 0,
          // actions: [Icon(Icons.cleaning_services_rounded)],
        ),
        body: Container(
          padding: EdgeInsets.all(15),
          child: Column(
            children: [
              Container(
                child: Expanded(
                  child: RefreshIndicator(
                    displacement: 40, //指示器显示时距顶部位置
                    color: Colors.greenAccent, //指示器颜色，默认ThemeData.accentColor
                    backgroundColor:
                        Colors.white, //指示器背景颜色，默认ThemeData.canvasColor
                    notificationPredicate: defaultScrollNotificationPredicate,
                    onRefresh: () async {
                      return await _loadUserInfo();
                    },
                    child: ListView(
                      shrinkWrap: true,
                      children: [
                        Container(
                          child: Column(
                            children: topWidget,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              InkWell(
                onTap: () {
                  Get.to(GetMessageWidget(
                    type: 8,
                  )).then((value) async {
                    await _loadUserInfo();
                  });
                },
                child: Container(
                  margin: EdgeInsets.only(top: 10, bottom: 20),
                  width: double.infinity,
                  alignment: Alignment.center,
                  child: Text(
                    "新建群发",
                    style: TextStyle(
                        fontSize: sp(15), color: ZdColor.rgba(0, 220, 169, 1)),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    }
  }

  @override
  void initState() {
    super.initState();

    _loadUserInfo();
    //  getData();
  }

  GroupMessageListModel someMessageModel;

  /// 加载当前登录用户信息
  _loadUserInfo() async {
    await TencentImSDKPlugin.v2TIMManager.getLoginUser().then((value) async {
      await TencentImSDKPlugin.v2TIMManager
          .getUsersInfo(userIDList: [value.data]).then((values) async {
        someMessageModel = await SaveSomeMessageDao.querySomeMessages(
            userId: values.data[0].userID);
        getDatas();
      });
    });
  }

  getDatas() {
    topWidget = [];
    if (someMessageModel == null || someMessageModel.data == null) {
      topWidget.add(SizedBox());
      setState(() {});
    } else {
      someMessageModel.data.forEach((element) {
        topWidget.add(
          Container(
            height: 60,
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(top: 15),
            color: ZdColor.rgba(249, 249, 249, 1),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  element.receiver.toString() + "位收信人:",
                  style: TextStyle(
                      fontSize: sp(13), color: ZdColor.rgba(153, 153, 153, 1)),
                ),
                Text(
                  element.nickNames,
                  style: TextStyle(
                      fontSize: sp(13), color: ZdColor.rgba(153, 153, 153, 1)),
                ),
              ],
            ),
          ),
        );

        if (element.contextType == 1) {
          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 194,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                        padding: EdgeInsets.all(20),
                        alignment: Alignment.topLeft,
                        child: Text(element.context)),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }

                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
        if (element.contextType == 2) {
          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 194,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: width(100),
                      padding: EdgeInsets.all(16),
                      margin: EdgeInsets.only(top: 15),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(22),
                        color: ZdColor.rgba(245, 245, 245, 1),
                      ),
                      alignment: Alignment.topLeft,
                      child: Row(
                        children: [
                          Icon(
                            Icons.keyboard_voice_rounded,
                            size: 17,
                          ),
                          Text(
                            element.size.toString() + "s",
                            style: TextStyle(fontSize: sp(12)),
                          ),
                        ],
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }

                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
        if (element.contextType == 10 || element.contextType == 5) {
          List<V2TimImage> a = [];
          if (element.fileInfoList != null) {
            a.add(V2TimImage(type: 1, url: element.fileInfoList[0].oImages));
          }

          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 300,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 15,
                    ),
                    InkWell(
                      onTap: () {
                        Get.to(PhotoViewGalleryScreen(
                          images: a, //传入图片list
                          index: 0, //传入当前点击的图片的index
                          heroTag: "1", //传入当前点击的图片的hero tag （可选）
                        ));
                      },
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(20),
                        child: Container(
                            width: width(140),
                            height: width(140),
                            child: CachedNetworkImage(
                              imageUrl: element.fileInfoList != null
                                  ? element.fileInfoList[0].oImages
                                  : "",
                              fit: BoxFit.cover,
                            )),
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }

                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
        if (element.contextType == 4) {
          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 300,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 15,
                    ),
                    Stack(
                      children: [
                        InkWell(
                          onTap: () {
                            Get.to(() => VideoMessage(
                                  url: element.context,
                                  snapshotUrl: element.oImages,
                                  type: 2,
                                ));
                          },
                          child: ClipRRect(
                              borderRadius: BorderRadius.circular(7),
                              // clipBehavior: ,
                              child: Container(
                                height: width(140),
                                width: width(140),
                                child: CachedNetworkImage(
                                  imageUrl: element.oImages,
                                  fit: BoxFit.cover,
                                ),
                              )),
                        ),
                        Positioned(
                          child: InkWell(
                            onTap: () {
                              Get.to(() => VideoMessage(
                                    url: element.context,
                                    snapshotUrl: element.oImages,
                                    type: 2,
                                  ));
                            },
                            child: Container(
                                height: width(140),
                                width: width(140),
                                child: Icon(
                                  Icons.play_circle_outline,
                                  color: Colors.white,
                                  size: 40,
                                )),
                          ),
                        ),
                      ],
                    ),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }
                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
      });
    }
    setState(() {});
  }

  List<Widget> topWidget = [];
  List<Widget> contextWidget;
  List<String> nickName;
  List<String> userId;
  getData() {
    topWidget = [];
    if (widget.someMessageModel == null ||
        widget.someMessageModel.data == null) {
      topWidget.add(SizedBox());
      setState(() {});
    } else {
      widget.someMessageModel.data.forEach((element) {
        topWidget.add(
          Container(
            height: 60,
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(top: 15),
            color: ZdColor.rgba(249, 249, 249, 1),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  element.receiver.toString() + "位收信人:",
                  style: TextStyle(
                      fontSize: sp(13), color: ZdColor.rgba(153, 153, 153, 1)),
                ),
                Text(
                  element.nickNames,
                  style: TextStyle(
                      fontSize: sp(13), color: ZdColor.rgba(153, 153, 153, 1)),
                ),
              ],
            ),
          ),
        );

        if (element.contextType == 1) {
          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 194,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                        padding: EdgeInsets.all(20),
                        alignment: Alignment.topLeft,
                        child: Text(element.context)),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }

                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
        if (element.contextType == 2) {
          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 194,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: width(100),
                      padding: EdgeInsets.all(16),
                      margin: EdgeInsets.only(top: 15),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(22),
                        color: ZdColor.rgba(245, 245, 245, 1),
                      ),
                      alignment: Alignment.topLeft,
                      child: Row(
                        children: [
                          Icon(
                            Icons.keyboard_voice_rounded,
                            size: 17,
                          ),
                          Text(
                            element.size.toString() + "s",
                            style: TextStyle(fontSize: sp(12)),
                          ),
                        ],
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }

                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
        if (element.contextType == 3 || element.contextType == 5) {
          List<V2TimImage> a = [];
          a.add(V2TimImage(type: 1, url: element.context));
          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 300,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 15,
                    ),
                    InkWell(
                      onTap: () {
                        Get.to(PhotoViewGalleryScreen(
                          images: a, //传入图片list
                          index: 0, //传入当前点击的图片的index
                          heroTag: "1", //传入当前点击的图片的hero tag （可选）
                        ));
                      },
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(20),
                        child: Container(
                            width: width(140),
                            height: width(140),
                            child: CachedNetworkImage(
                              imageUrl: element.context,
                              fit: BoxFit.cover,
                            )),
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }

                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
        if (element.contextType == 4) {
          topWidget.add(ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: Container(
                alignment: Alignment.topLeft,
                height: 300,
                margin: EdgeInsets.only(top: 15),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 15,
                    ),
                    Stack(
                      children: [
                        InkWell(
                          onTap: () {
                            Get.to(() => VideoMessage(
                                  url: element.context,
                                ));
                          },
                          child: ClipRRect(
                              borderRadius: BorderRadius.circular(7),
                              // clipBehavior: ,
                              child: Container(
                                height: width(140),
                                width: width(140),
                                child: CachedNetworkImage(
                                  imageUrl: element.oImages,
                                  fit: BoxFit.cover,
                                ),
                              )),
                        ),
                        Positioned(
                          child: Container(
                              height: width(140),
                              width: width(140),
                              child: Icon(
                                Icons.play_circle_outline,
                                color: Colors.white,
                                size: 40,
                              )),
                        ),
                      ],
                    ),
                    InkWell(
                      onTap: () {
                        List<AddressBookModel> someUser = [];
                        nickName = [];
                        userId = [];
                        nickName = element.nickNames.split(",");
                        userId = element.toUsers.split(",");
                        for (int i = 0; i < nickName.length; i++) {
                          print(nickName[i]);
                          someUser.add(AddressBookModel(
                              name: nickName[i], userId: userId[i]));
                        }

                        Get.to(() => SendMessageAllPage(
                              someUser: someUser,
                            )).then((value) async {
                          // showToast(value.toString());
                          await _loadUserInfo();
                        });
                      },
                      child: Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(3),
                        width: 70,
                        height: 24,
                        margin: EdgeInsets.only(
                          left: width(260),
                          top: 90,
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: ZdColor.rgba(0, 220, 169, 1))),
                        child: Text(
                          "再发一条",
                          style: TextStyle(
                              color: ZdColor.rgba(0, 220, 169, 1),
                              fontSize: sp(12)),
                        ),
                      ),
                    ),
                  ],
                ),
              )));
        }
      });
      setState(() {});
    }
  }
}
